
package com.bbh.plugin.single.privilege.demo.controller;

import com.bbh.plugin.single.privilege.core.entity.LanguageInfo;
import com.bbh.plugin.single.privilege.core.entity.LanguageInfoLabel;
import com.bbh.plugin.single.privilege.core.enums.LanguageEnum;
import com.bbh.common.base.vo.GridVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.domain.Message;
import com.bbh.common.domain.StatusType;
import com.bbh.common.utils.IdUtils64;
import com.bbh.plugin.single.privilege.core.service.LanguageInfoLabelService;
import com.bbh.plugin.single.privilege.core.service.LanguageInfoService;
import com.bbh.plugin.single.privilege.core.service.LanguageLabelService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.bbh.common.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;


@Controller
@RequestMapping("/languageInfo")
public class LanguageInfoController extends BasicController {
    @Autowired
    private LanguageInfoService languageInfoService;
    @Autowired
    private LanguageLabelService languageLabelService;
    @Autowired
    private LanguageInfoLabelService languageInfoLabelService;

    @RequestMapping(value = VIEW_FILED + "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        model.addAttribute("languages", EnumSet.allOf(LanguageEnum.LANGUGGE.class));
        model.addAttribute("labels", languageLabelService.selectAll());
        return "single/language/languageInfo";
    }


    @RequestMapping(value = VIEW_FILED + "/list")
    public
    @ResponseBody
    GridVo list(HttpServletRequest request, String propertyKey, String propertyValue, String language, String labels) throws ServletRequestBindingException {
        Condition condition = new Condition();
        GridVo vo = new GridVo();
        if (StringUtils.isNotEmpty(propertyKey)) {
            condition.put("propertyKey", Condition.LIKE, propertyKey);
        }
        if (StringUtils.isNotEmpty(propertyValue)) {
            condition.put("propertyValue", Condition.LIKE, propertyValue);
        }
        if (StringUtils.isNotEmpty(language)) {
            condition.put("language", Condition.EQ, language);
        }
        Page<LanguageInfo> pageResult = null;
        if (StringUtils.isNotEmpty(labels)) {
            String[] labelIds = labels.split(",");
            condition.put("labels",labelIds);
        }
        pageResult = languageInfoService.selectPage(condition, initPageRequest(request, PAGE_NUMBER, PAGE_ROWS));



        return new GridVo(pageResult);
    }

    @RequestMapping(value = INSERT_FILED + "/insert")
    public
    @ResponseBody
    Message insert(@Valid LanguageInfo languageInfo, String labels) {
        Date date = new Date();
        if (StringUtils.isEmpty(languageInfo.getPropertyKey()) || StringUtils.isEmpty(languageInfo.getPropertyValue()) || StringUtils.isEmpty(labels) || StringUtils.isEmpty(languageInfo.getLanguage())) {
            return new Message(StatusType.ERROR.getKey(), "参数错误");
        }
        languageInfo.setModifyTime(date);
        languageInfo.setModifyBy(getLoginName());
        languageInfo.setCreateBy(getLoginName());
        languageInfo.setCreateTime(date);
        languageInfo.setId(IdUtils64.getInstance().getStringId());
        languageInfoService.insert(languageInfo, labels);
        return new Message(StatusType.SUCCESS.getKey(), "保存成功");
    }

    /**
     * 跳转到edit页面
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED + "/edit")
    public String edit(Model model, String type, String id) {
        if ("edit".equals(type)) {
            if (StringUtils.isNotEmpty(id)) {
                LanguageInfo languageInfo = languageInfoService.selectByPrimaryKey(id);
                model.addAttribute("languageInfo", languageInfo);
                Condition condition = new Condition();
                condition.putEquals("languageInfoId", id);
                List<LanguageInfoLabel> languageInfoLabels = languageInfoLabelService.selectList(condition, null);
                model.addAttribute("labelIds", languageInfoLabels);
            }
            model.addAttribute(OPERATE_TYPE, "edit");
        }
        if ("add".equals(type)) {
            model.addAttribute(OPERATE_TYPE, "add");
        }
        model.addAttribute("languages", EnumSet.allOf(LanguageEnum.LANGUGGE.class));
        Condition condition = new Condition();
        condition.putEquals("isEnabled", "1");
        model.addAttribute("labels", languageLabelService.selectList(condition, null));
        return "single/language/languageInfoEdit";
    }

    /**
     * update
     *
     * @return
     */
    @RequestMapping(UPDATE_FILED + "/update")
    public
    @ResponseBody
    Message update(@Valid LanguageInfo languageInfo, String labels) {
        Assert.notNull(languageInfo.getId(), "参数错误");
        LanguageInfo updateLanguageInfo = languageInfoService.selectByPrimaryKey(languageInfo.getId());
        if (updateLanguageInfo != null) {
            updateLanguageInfo.setModifyBy(getLoginName());
            updateLanguageInfo.setModifyTime(new Date());
            updateLanguageInfo.setPropertyKey(languageInfo.getPropertyKey());
            updateLanguageInfo.setPropertyValue(languageInfo.getPropertyValue());
            updateLanguageInfo.setDescription(languageInfo.getDescription());
            updateLanguageInfo.setLanguage(languageInfo.getLanguage());
            languageInfoService.update(updateLanguageInfo, labels);
            return new Message(StatusType.SUCCESS.getKey(), "更新成功");
        }
        return new Message(StatusType.ERROR.getKey(), "更新失败");
    }

    @RequestMapping(DELETE_FILED + "/delete")
    public
    @ResponseBody
    Message delete(@RequestParam(value = "ids[]") String[] ids) {
        if (ArrayUtils.isNotEmpty(ids)) {
            languageInfoService.delete(ids);
            return new Message(StatusType.SUCCESS.getKey(), "删除成功");
        }
        return new Message(StatusType.ERROR.getKey(), "删除失败");

    }

}